var AllLottery = AllLottery || {};

AllLottery.allwiners = []
AllLottery.allwinersIndex = 0;
AllLottery.curPage = 0;
AllLottery.curTimeout;

AllLottery.getWinnersInfo = function () {
    $.ajax({
        url: '/overview/getLotteryWinnersInfo',
        method: 'GET'
    }).done(function (response) {
        if (response.code === 0) {
            AllLottery.allwiners = response.data;

            //刷新队列
            clearTimeout(AllLottery.curTimeout);
            clearcontent();
            refreshPage();
        } else {
        }
    });
};

AllLottery.initData = function () {
    AllLottery.getWinnersInfo();
};

function refreshPage() {
    AllLottery.allwinersIndex = 0;
    var page = 0;
    if(AllLottery.allwiners.length % 40 === 0){
        console.log('refreshpage40')
        page = AllLottery.allwiners.length/40;
    }else{
        console.log('refreshpage40+')
        page = Math.floor(AllLottery.allwiners.length/40)+1;
    }
    for(var i = 0; i<page; i++){
        var content = $('<div>');
        content.addClass('content');
        AllLottery.showRandomPicTest(content);
        $('.allcontent').append(content)
    }

    looppic();
}

function clearcontent() {
    var childs = $('.allcontent').children();
    console.log(childs);
    for(var i=0; i< childs.length; i++){
        childs.remove();
    }
}

function looppic() {
    function showPicdiv() {
        if(AllLottery.curPage < $('.allcontent').children().length){
        }else {
            AllLottery.curPage = 0;
        }
        //显示出来
        $($('.allcontent').children()[AllLottery.curPage]).css('visibility','visible');
        //增加一个2s的动画
        $($('.allcontent').children()[AllLottery.curPage]).animate({
                opacity:0.1
            },20020,function () {
            $(this).css('opacity','1');
        });
        //2s后再隐藏掉
        AllLottery.curTimeout = setTimeout(function () {
            $($('.allcontent').children()[AllLottery.curPage]).css('visibility','hidden');
            AllLottery.curPage++;
            showPicdiv();
        },20000)

    }
    showPicdiv();

}


AllLottery.showRandomPicTest = function(content) {
    var resultDom = content;

    var n = 4;
    var m = 10;

    var cardXInit = 2;
    var cardXwidth = (100-2*cardXInit)/m;
    var cardYHeight = cardXwidth*19.5/9;
    var cardYInit = (100-(n*cardYHeight))/2;

    var cardX = cardXInit;
    var cardY = cardYInit;

    for(var i = 0 ; i < 40 ; i++) {
        if(AllLottery.allwinersIndex < AllLottery.allwiners.length){

            var d = AllLottery.allwiners[AllLottery.allwinersIndex];
            AllLottery.allwinersIndex++;

            var cardbox = $('<div>');
            cardbox.addClass('alllotterybox')
            var card = $('<div>');
            card.addClass('recordcard');
            card.html($('#avard-record-card-tmpl').html());
            card.find('.photo').attr('src', d.avatar || '/static/images/default.jpg');
            card.find('.photo').addClass("recordphoto");
            card.find('.textdes').addClass('recordcardtextdes').text(d.name);
            card.find('.greetings').addClass('recordcardgreetings').text(d.department);
            card.find('.description').addClass('recordcarddescription').text(d.lottery);


            cardbox.append(card);

            var pichead = cardbox[0];
            var picheadJQ = $(pichead);

            if(cardX+cardXwidth <= (100-cardXInit) ) {
                picheadJQ.css('left', cardX + '%').css('top', cardY + '%');
                cardX += cardXwidth;
            } else {
                cardX = cardXInit;
                if(cardY+cardYHeight <= (100-cardYInit)) {
                    cardY += cardYHeight;
                } else {
                    cardY = cardYInit;
                }
                picheadJQ.css('left', cardX + '%').css('top', cardY + '%');
                cardX += cardXwidth;
            }
            $(resultDom).css({
                width: '100%',
                height: '100%',
                left: '0%',
                top: '0%'
            });

            resultDom.append(pichead);
        }
    }
}


function showCardFlow(content) {
    isStopCardLoop = false;
    var childrenwidth = $(content.children()[0]).width();
    var childNum = content.children().length;

    var imglastsecond_x = -(childrenwidth * (childNum - 1));//倒数第一个子元素的左上角x坐标
    content.css('left', imglastsecond_x+"px");//初始化移动到倒数第一个
    var next = imglastsecond_x + childrenwidth;//下一步要移动到倒数第二个
    var i = childNum - 2;

    function scrollToBottom(argument) {
        content.animate({
            'left': next+'px'
            },1000, function() {
                if(isStopCardLoop){
                    next+=childrenwidth;
                    content.animate({
                        'left': next+'px'
                    },3000);
                    return;
                }

                if(i===0){
                    //移动到了顶部，顶部和倒数第一个是一样的，所以应该直接跳到倒数第一个再进行下一轮循环；
                    content.css('left',imglastsecond_x+'px');//重新移动到倒数第一个
                    next = imglastsecond_x+childrenwidth;//下一步就是倒数第二个
                    i = childNum-2;//i也重新回到初始
                    scrollToBottom();
                }else{
                    i -=1;
                    next+=childrenwidth;
                    scrollToBottom();
                }
        });
    }
    scrollToBottom();
}


AllLottery.initData();